// pages/vcheck/vcheck.js
const constant = require('../../constant');
const util = require('../../utils/util');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    headImg: ['../image/rz01.png'],
    jszImg: ['../image/rz02.png'],
    xszImg: ['../image/rz03.png'],
    sfzImgFront: ['../image/rz04.png'],
    sfzImgBack: ['../image/rz05.png'],
    imgArray: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  chooseImg: function(e){
    let that = this;
    var type = e.currentTarget.dataset.type;
    var count = 1;
    //if (type == '1')count =2;
    wx.chooseImage({
      count: count,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        if (type == '0') that.setData({ headImg: tempFilePaths });
        else if (type == '11') that.setData({ sfzImgFront: tempFilePaths });
        else if (type == '12') that.setData({ sfzImgBack: tempFilePaths });
        else if (type == '2') that.setData({ jszImg: tempFilePaths });
        else if (type == '3') that.setData({ xszImg: tempFilePaths });
      }
    })
  },
  rzSubmit: function() {
    var that = this;
    var imgarray = [];
    if (that.data.headImg.length == 1) {
      var data = {'type': '0', 'img': that.data.headImg[0]};
      imgarray.push(data);
    } else {
      wx.showToast({
        title: '个人头像不能为空',
        duration: 1000,
        success(){ return }
      })
    }
    if (that.data.sfzImgFront.length == 1) {
      var data = { 'type': '1', 'img': that.data.sfzImgFront[0] };
      imgarray.push(data);
    } else {
      wx.showToast({
        title: '身份证正面照不能为空',
        duration: 1000,
        success() { return }
      })
    }
    if (that.data.sfzImgBack.length == 1) {
      var data = { 'type': '1', 'img': that.data.sfzImgBack[0] };
      imgarray.push(data);
    } else {
      wx.showToast({
        title: '身份证背面照不能为空',
        duration: 1000,
        success() { return }
      })
    }
    if (that.data.jszImg.length == 1) {
      var data = { 'type': '2', 'img': that.data.jszImg[0] };
      imgarray.push(data);
    } else {
      wx.showToast({
        title: '驾驶证不能为空',
        duration: 1000,
        success() { return }
      })
    }
    if (that.data.xszImg.length == 1) {
      var data = { 'type': '3', 'img': that.data.xszImg[0] };
      imgarray.push(data);
    } else {
      wx.showToast({
        title: '行驶证不能为空',
        duration: 1000,
        success() { return }
      })
    }
    var successUp = 0; //成功
    var failUp = 0; //失败
    var length = 5; //总数
    var count = 0; //第几张
    that.uploadFile(imgarray, successUp, failUp, count, length);
  },
  uploadFile: function(imgPaths, successUp, failUp, count, length) {
    var that = this;
    wx.showLoading({
      title: '正在上传第' + count + '张',
    })
    wx.uploadFile({
      url: constant.config.url_request + 'Upload/UserCert',
      filePath: imgPaths[count].img,
      name: 'file',
      formData: {
        'type': imgPaths[count].type,
        'token': wx.getStorageSync("token")
      },
      success: function (e) {
        successUp++;
      },
      fail: function (e) {
        failUp++;
      },
      complete: function (e) {
        count++;
        if (count == length) {
          //上传完毕
          console.log('上传成功' + successUp + ',' + '失败' + failUp);
          wx.showToast({
            title: '上传成功' + successUp,
            icon: 'success',
            duration: 2000,
            success(){
              setTimeout(function () {
                wx.switchTab({
                  url: '../my/my'
                })
              })
            }
          })
        } else {
          //上传下一张
          that.uploadFile(imgPaths, successUp, failUp, count, length);
          console.log('正在上传第' + count + '张');
        }
      }
    })
  }
})